﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GamePanel : BasePanel<GamePanel>
{
    public Tank tank;
    public UIToggle togSoundEffect;
    public UILabel labName;
    public UIButton btnSetName;
    public UIPopupList popupList;
    public Light light;
    public UISlider volumeSlider;
    public UIProgressBar esHpProgressBar;
    public UIButton btnBag;
    public UISprite controller;
    void Start()
    {
        togSoundEffect.value = GameDataMgr.Instance.musicData.soundEffect;
        togSoundEffect.onChange.Add(new EventDelegate(OnSoundEffectChange));
        labName.text = GameDataMgr.Instance.playerData.name;
        btnSetName.onClick.Add(new EventDelegate(() => {
            SetNamePanel.Instance.InitName(labName.text);
            SetNamePanel.Instance.Show();
        }));
        volumeSlider.value = GameDataMgr.Instance.musicData.volume;
        popupList.onChange.Add(new EventDelegate(()=>
        {
            light.intensity = popupList.value == "白天" ? 1f : 0.5f;
        }));
        volumeSlider.onChange.Add(new EventDelegate(OnVolumeChange));
        esHpProgressBar.gameObject.SetActive(false);
        btnBag.onClick.Add(new EventDelegate(() => {
            BagPanel.Instance.Show();
        }));

        #region 摇杆功能
        UIEventListener listener = UIEventListener.Get(controller.gameObject);
        //监听拖曳 让图片跟着鼠标动
        listener.onDrag = (GameObject obj, Vector2 vec) =>
        {
            //拖曳的时候 对象不停的去改变位置 加上位置的变量
            controller.transform.localPosition += new Vector3(vec.x, vec.y, 0);
            //判断极限位置 不能随便拖
            if (controller.transform.localPosition.magnitude > 177)
                controller.transform.localPosition = controller.transform.localPosition.normalized * 177;

            //拖曳时让 玩家移动
            //我们传入摇杆的方向向量 再内部转换成世界坐 xz平面上的向量
            tank.Move(controller.transform.localPosition.normalized);
        };
        //监听拖曳 让图片位置重置
        listener.onDragEnd = (GameObject obj) =>
        {
            tank.StopMove();
            controller.transform.localPosition = Vector3.zero;
        };
        #endregion
    }

    public void Fire()
    {
        tank.Fire();
    }

    public void OnSoundEffectChange()
    {
        GameDataMgr.Instance.musicData.SetSoundEffect(togSoundEffect.value);
    }

    public void OnVolumeChange()
    {
        GameDataMgr.Instance.musicData.SetVolume(volumeSlider.value);
    }

    public void SetName(string value)
    {
        labName.text = value;
    }

    void Update()
    {
        //按下与长按
        if (Input.GetMouseButton(0))
        {
            if (esHpProgressBar.gameObject.activeSelf == false)
            {
                esHpProgressBar.value = 0;
                esHpProgressBar.gameObject.SetActive(true);
            }
            esHpProgressBar.value += Time.deltaTime/5;
            if (esHpProgressBar.value >= 1)
            {
                esHpProgressBar.value = Time.deltaTime/5;
                tank.CostHp(10);
            }
        }
        //抬起
        else if (Input.GetMouseButtonUp(0))
        {
            esHpProgressBar.value = 0;
            esHpProgressBar.gameObject.SetActive(false);
        }
    }
}
